package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamRefAssetPartDTO;
import com.jcfk.eam.domain.query.base.EamRefAssetPartQuery;

import com.jcfk.eam.service.base.EamRefAssetPartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 资产备件关联关系
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2025-08-26
 */
@Api(tags = {"资产备件关联关系管理"})
@RestController
@RequestMapping("eamrefassetpart")
public class EamRefAssetPartController {
    @Autowired
    private EamRefAssetPartService eamRefAssetPartService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRefAssetPartQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产备件关联关系")
    @PostMapping("/queryByPage")
    public RetPage<EamRefAssetPartDTO> queryByPage(@RequestBody ReqPageParam<EamRefAssetPartQuery> requestParam) {
        DataPage<EamRefAssetPartDTO> page = new DataPage<EamRefAssetPartDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamRefAssetPartDTO.class);
        page = eamRefAssetPartService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     *  资产备件关联关系
     * 根据id 查询详情
     */
    @ApiOperation("查询资产备件关联关系")
    @GetMapping("/get/{id}")
    public RetResult<EamRefAssetPartDTO> get(@PathVariable("id") String id) {
            EamRefAssetPartDTO item = eamRefAssetPartService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产备件关联关系
     */
    @ApiOperation("保存资产备件关联关系")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产备件关联关系管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamRefAssetPartDTO eamRefAssetPart) {
        // 数据校验
        RetResult ret = eamRefAssetPartService.isValid(eamRefAssetPart);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamRefAssetPart.getId())){
            // 新增
            eamRefAssetPartService.insert(eamRefAssetPart);
        } else{
            // 更新
            eamRefAssetPartService.update(eamRefAssetPart);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产备件关联关系
     */
    @ApiOperation("批量删除资产备件关联关系")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产备件关联关系管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamRefAssetPartDTO eamRefAssetPart) {
        return RetResult.ok(eamRefAssetPartService.logicDeleteList(eamRefAssetPart));
    }

    /**
     *  删除资产备件关联关系
     */
    @ApiOperation("删除资产备件关联关系")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产备件关联关系管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamRefAssetPartService.deleteitem(id);
        return RetResult.ok();
    }
}
